#include "sclib.h"
#include <QLabel>

// 实现 class SCLibPrivate
class SCLib::SCLibPrivate
{
    Q_DISABLE_COPY(SCLibPrivate)

    // 使SCLibPrivate可以访问SCLib中的成员
    // 通过定义 Q_Q(SCLib)来实现
    SCLib* const q_ptr{};
    Q_DECLARE_PUBLIC(SCLib)

public:
    SCLibPrivate(SCLib* parent)
        : q_ptr(parent)
    {

        m_val = 10;
        m_label_val = new QLabel(QString("m_val=%1").arg(m_val), parent);
        m_label_val->move(10,10);

        m_name = "my name";
        m_label = new QLabel(m_name, parent);
        m_label->move(10,40);
    }

    void changeVal()
    {
        Q_Q(SCLib);

        m_label_val->setText(QString("m_val=%1").arg(q->getVal()));
    }

private:
    QString m_name;
    int m_val;
    QLabel *m_label;
    QLabel * m_label_val;
};

SCLib::SCLib(QWidget* parent)
    : QWidget(parent)
    , d_ptr(new SCLibPrivate(this))
{

    // 设置背景颜色
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::darkCyan);
    setPalette(pal);

    // 设置label字体颜色
    setStyleSheet("QLabel { color: white; }");
}

void SCLib::changeName(const QString& name)
{
    Q_D(SCLib);
    d->m_label->setText(name);
    d->changeVal();
}
